package com.yyds.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyds.common.MyJSON;
import com.yyds.mapper.PersonResidentVoMapper;
import com.yyds.mapper.PersonStaffMapper;
import com.yyds.pojo.PersonStaff;
import com.yyds.service.PersonStaffService;
import com.yyds.vo.EchartBarVo;
import com.yyds.vo.PersonStaffVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 花君瑜
 * @date 2022/10/14 18:43:40
 * @desc
 **/
@Service("/personStaffService")
public class PersonStaffServiceImpl implements PersonStaffService {
    @Autowired
    private PersonStaffMapper personStaffMapper;
    @Autowired
    private PersonResidentVoMapper personResidentVoMapper;


    @Override
    public MyJSON<PersonStaffVo> queryAllPersonStaffService(Map<String, Object> params) {
        //mybatis分页 插件的使用
        int page = Integer.parseInt(params.get("page") + "");
        int limit = Integer.parseInt(params.get("limit") + "");

        PageHelper.startPage(page, limit);
        List<PersonStaffVo> personStaffs = personStaffMapper.queryAllPersonStaffMapper(params);
        System.out.println("List<PersonStaff> personResidents ="+personStaffs);
        //mybatis自带的分页数据结构
        PageInfo<PersonStaffVo> personStaffPageInfo = new PageInfo<>(personStaffs);
        // 把mybatis 的分页数据结构中的 数据 封装到 layui的 分页数据结构中
        MyJSON<PersonStaffVo> personStaffMyJSON = new MyJSON<>();
        personStaffMyJSON.setCode(0);
        personStaffMyJSON.setMsg("居民信息分页列表数据");
        personStaffMyJSON.setCount(personStaffPageInfo.getTotal());
        personStaffMyJSON.setData(personStaffPageInfo.getList());
        System.out.println(personStaffMyJSON);
        return personStaffMyJSON;
    }

    @Override
    public Integer deletePersonStaffService(Integer staff_id) {
        return personStaffMapper.deletePersonStaffMapper(staff_id);
    }

    @Override
    public Integer deleteAllPersonStaffService(String str) {
        //1-2-3
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
        }
        return personStaffMapper.deleteAllPersonStaffMapper(list);
    }

    @Override
    public Integer addPersonStaffService(PersonStaffVo personStaffVo) {
        PersonStaff personStaff = new PersonStaff();
        //根据地区名 查 地区id
        Integer region_id = personStaffMapper.queryRegionIdMapper(personStaffVo.getRegion_name());
        //将地区id 存入personStaff中
        personStaff.setRegion_id(region_id);
        //根据居民名称 查 居民id
        Integer resident_id = personResidentVoMapper.queryVaccineIdMapper(personStaffVo.getResident_name());
        //将人员id 存入personStaff中
        personStaff.setResident_id(resident_id);
        return personStaffMapper.addPersonStaffMapper(personStaff);
    }

    @Override
    public Integer updatePersonStaffService(PersonStaffVo personStaffVo,Integer staff_id) {
        PersonStaff personStaff = new PersonStaff();
        //根据地区名 查 地区id
        Integer region_id = personStaffMapper.queryRegionIdMapper(personStaffVo.getRegion_name());
        //将地区id 存入personStaff中
        personStaff.setRegion_id(region_id);
        //根据居民名称 查 居民id
        Integer resident_id = personResidentVoMapper.queryVaccineIdMapper(personStaffVo.getResident_name());
        //将人员id 存入personStaff中
        personStaff.setResident_id(resident_id);
        //将行政人员id 存入personStaff中
        personStaff.setStaff_id(staff_id);
        return personStaffMapper.updatePersonStaffMapper(personStaff);
    }

    @Override
    public List<String> queryAllRegionNameService() {
        return personStaffMapper.queryAllRegionNameMapper();
    }

    @Override
    public List<EchartBarVo> queryPersonStaffService() {
        return personStaffMapper.queryPersonStaffMapper();
    }
}
